package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.innovation.chengzhangriji.R;

public class SmoothTopBar extends LinearLayout implements ViewPager.OnPageChangeListener, View.OnClickListener {

    public interface SelectItemListener {

        void selectItem(int position);

    }

    private Paint mPaint;

    private Rect rect;

    private int rectWidth;

    private int rectHeight;

    private int offsetX;

    private int colorBlue;

    private int colorBlack;

    private CuteTextView mCtvLeft;

    private CuteTextView mCtvRight;

    private SelectItemListener mSelectItemListener;

    public SmoothTopBar(Context context) {
        super(context);
        init(context);
    }

    public SmoothTopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SmoothTopBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        switch (position) {
            case 0:
                offsetX = (int) (rectWidth * positionOffset);
                break;
            case 1:
                offsetX = rectWidth - (int) (rectWidth * positionOffset);
                break;
        }
        invalidate();
    }

    @Override
    public void onPageSelected(int position) {
        switch (position) {
            case 0:
                offsetX = 0;
                mCtvLeft.setTextColor(colorBlue);
                mCtvRight.setTextColor(colorBlack);
                break;
            case 1:
                offsetX = rectWidth;
                mCtvLeft.setTextColor(colorBlack);
                mCtvRight.setTextColor(colorBlue);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        if(mSelectItemListener==null){
            return;
        }
        switch (v.getId()) {
            case R.id.left_title:
                mSelectItemListener.selectItem(0);
                break;
            case R.id.right_title:
                mSelectItemListener.selectItem(1);
                break;
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectWidth = getWidth() / 2;
        rect.left = offsetX;
        rect.top = getHeight() - rectHeight;
        rect.right = getWidth() / 2 + offsetX;
        rect.bottom = getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        rect.left = offsetX;
        rect.right = getWidth() / 2 + offsetX;
        canvas.drawRect(rect, mPaint);
    }

    public void setSelectItemListener(SelectItemListener selectItemListener) {
        mSelectItemListener = selectItemListener;
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_smoothtopbar, this, true);

        colorBlue = getResources().getColor(R.color.text_color_blue);
        colorBlack = getResources().getColor(R.color.text_color_black);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(colorBlue);
        rect = new Rect();
        rectHeight = (int) getResources().getDimension(R.dimen.smooth_top_bar_rect_height);

        mCtvLeft = (CuteTextView) findViewById(R.id.left_title);
        mCtvRight = (CuteTextView) findViewById(R.id.right_title);

        mCtvLeft.setOnClickListener(this);
        mCtvRight.setOnClickListener(this);

        setWillNotDraw(false);
    }
}
